package com.hrt.datasender.web.processstrategy;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.hrt.datasender.process.strategy.IProcessStrategy;
import com.hrt.datasender.process.strategy.ProcessStrategyLoader;
import com.hrt.datasender.process.strategy.model.ProcessStrategyModel;

@Service
public class ProcessStrategyService {

    private static volatile ProcessStrategyService instance;

    public static ProcessStrategyService getInstance() {
        if (instance == null) {
            synchronized (ProcessStrategyService.class) {
                if (instance == null) {
                    instance = new ProcessStrategyService();
                }
            }
        }
        return instance;
    }

    public List<ProcessStrategyModel> getList()
            throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {

        List<IProcessStrategy> processStrategys = ProcessStrategyLoader.getInstance().getExistStrategy();
        List<ProcessStrategyModel> models = new ArrayList<>();

        for (IProcessStrategy strategy : processStrategys) {
            ProcessStrategyModel model = new ProcessStrategyModel();
            model.setStrategyId(strategy.getStrategyId());
            model.setStrategyName(strategy.getStrategyName());

            models.add(model);
        }

        return models;
    }

}
